package com.onenine.app.view;

import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.os.Build;
import android.support.v4.app.NotificationCompat;

import com.onenine.app.R;
import com.onenine.app.listener.NotificationCountReceiver;

/**
 * 创建时间：2018/8/21 0021 9:28
 * 创建人：fly
 * 功能描述：消息通知栏
 */
public class MsgNotification {

    private static final String CHANNEL_ID = "19区";
    private static final String NOTIFICATION_UNREAD_COUNT_FORMAT = "%s[%d条未读]";
    private NotificationManager mNotificationMgr;
    private NotificationCompat.Builder mNotificationBuilder;
    private Context mContext;
    private int mNotificationId;
    private boolean mAutoCount;

    public static class MsgContent {
        public String msgTitle;
        public String msgDesc;
        public long msgDate;
    }

    public MsgNotification(Context context, String uuid) {
        mContext = context;
        mNotificationId = uuid.hashCode();
        mNotificationMgr = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        mNotificationBuilder = new NotificationCompat.Builder(context);
        mNotificationBuilder.setSmallIcon(R.mipmap.ic_launcher)//设置小图标
                .setLargeIcon(BitmapFactory.decodeResource(Resources.getSystem(), R.mipmap.ic_launcher))
                .setDefaults(Notification.DEFAULT_ALL)
                .setTicker(mContext.getText(R.string.app_name))
                .setAutoCancel(true);
    }

    /**
     * 设置通知点击事件响应
     *
     * @param intent
     * @return
     */
    public MsgNotification setMsgIntent(Intent intent) {
        PendingIntent pendingIntent = PendingIntent.getActivity(mContext
                , mNotificationId, intent, PendingIntent.FLAG_ONE_SHOT);
        mNotificationBuilder.setContentIntent(pendingIntent);
        return this;
    }

    /**
     * 设置自动计数
     * 该方法的调用需要在
     * setMsgContent之前有效
     *
     * @return
     */
    public MsgNotification setAutoCount() {
        mAutoCount = true;
        NotificationCountReceiver.notifyAddNotification(mNotificationId);
        mNotificationBuilder.setDeleteIntent(PendingIntent.getBroadcast(mContext, mNotificationId
                , NotificationCountReceiver.getNotificationClearIntent(mContext, mNotificationId), PendingIntent.FLAG_ONE_SHOT));
        return this;
    }

    /**
     * 设置通知内容
     *
     * @param msgContent
     * @return
     */
    public MsgNotification setMsgContent(MsgContent msgContent) {
        if (msgContent != null) {
            if (mAutoCount) {
                int count = NotificationCountReceiver.getNotificationCountById(mNotificationId);
                if (count > 1) {
                    mNotificationBuilder.setContentTitle(String.format(NOTIFICATION_UNREAD_COUNT_FORMAT
                            , msgContent.msgTitle, count));
                } else {
                    mNotificationBuilder.setContentTitle(msgContent.msgTitle);
                }
            } else {
                mNotificationBuilder.setContentTitle(msgContent.msgTitle);
            }
            mNotificationBuilder.setContentText(msgContent.msgDesc);
        }
        return this;
    }

    /**
     * 显示通知
     * Android 8.0要使用通知渠道配置否则不能显示
     * notificationId为通知标识，相同标识会覆盖
     */
    public void showNotification() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            NotificationChannel channel = new NotificationChannel(CHANNEL_ID, CHANNEL_ID, NotificationManager.IMPORTANCE_HIGH);
            channel.setDescription(CHANNEL_ID);
            channel.enableLights(true);
            channel.setLightColor(Color.RED);
            channel.enableVibration(true);
            mNotificationMgr.createNotificationChannel(channel);
            mNotificationBuilder.setChannelId(CHANNEL_ID);
        }
        mNotificationMgr.notify(mNotificationId, mNotificationBuilder.build());
    }

}
